<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');

class RestaurantsControllerReviews extends JController
{
	function __construct($config = array())
	{
		parent::__construct($config);
		
		$this->registerTask('unpublish', 'publish');
		$this->registerTask('apply', 'save');
	}
	
	function edit()
	{
		JRequest::setVar('view', 'single');

		$this->display();
	}
	
	function add()
	{
		JRequest::setVar('view', 'single');

		$this->display();
	}

	function save()
	{
		global $option;

		$row =& JTable::getInstance('review', 'Table');

		if (!$row->bind(JRequest::get('post'))) 
		{
			JError::raiseError(500, $row->getError() );
		}

		$row->quicktake = JRequest::getVar( 'quicktake', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$row->review = JRequest::getVar( 'review', '', 'post', 'string', JREQUEST_ALLOWRAW );

		if(!$row->review_date)
		{
			$row->review_date = date( 'Y-m-d H:i:s' );
		}

		if (!$row->store()) 
		{
			JError::raiseError(500, $row->getError() );
		}
		
		$file = JRequest::getVar( 'audiofile', '', 'files', 'array' );

		if(isset($file['name']) && $file['name'] != '')
		{	
			jimport('joomla.filesystem.file');

			$ext = JFile::getExt($file['name']);

			$filename = JPATH_SITE . DS . 'components' . DS . $option . DS . 'audio' . DS . $row->id . '.' . $ext;

			if (!JFile::upload($file['tmp_name'], $filename)) {
				$this->setRedirect("index.php?option=$option", "Upload failed, check to make sure that /components/$option/audio exists and is writable.");
				return;
			}

		}
		
		if ($this->getTask() == 'apply') {
			$this->setRedirect('index.php?option=' . $option . '&task=edit&cid[]=' . $row->id, 'Changes Applied');
		} else {
			$this->setRedirect('index.php?option=' . $option, 'Review Saved');
		}
	}
	
	function publish()
	{
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		global $option;

		$cid = JRequest::getVar('cid', array());

		$row =& JTable::getInstance('review', 'Table');

		$publish = 1;

		if ($this->getTask() == 'unpublish') {
			$publish = 0;
		}

		if(!$row->publish($cid, $publish))
		{
			JError::raiseError(500, $row->getError() );
		}

		$s = '';

		if (count($cid) > 1) {
			$s = 's';
		}

		$msg = 'Review' . $s;

		if ($this->getTask() == 'unpublish') {
			$msg .= ' unpublished';
		} else {
			$msg .= ' published';
		}

		$this->setRedirect('index.php?option=' . $option, $msg);
	}
	
	function remove()
	{
		JRequest::checkToken() or jexit( 'Invalid Token' );

		global $option;

		$cid = JRequest::getVar('cid', array(0));

		$row =& JTable::getInstance('review', 'Table');

		foreach ($cid as $id) {
			$id = (int) $id;

			if (!$row->delete($id)) {
				JError::raiseError(500, $row->getError() );
			}
		}

		$s = '';

		if (count($cid) > 1) {
			$s = 's';
		}

		$this->setRedirect('index.php?option=' . $option, 'Review' . $s . ' deleted.');
	}
	
	function display()
	{
		$view = JRequest::getVar('view');
		
		if (!$view) {
			JRequest::setVar('view', 'all');
		}

		parent::display();
	}
}